package com.zelin.service.Impl;

import com.zelin.mapper.ClassesMapper;
import com.zelin.mapper.StudentMapper;
import com.zelin.pojo.Classes;
import com.zelin.pojo.Student;
import com.zelin.pojo.StudentExample;
import com.zelin.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class StudentServiceImpl implements StudentService {

    @Autowired
    private StudentMapper studentMapper;
    @Autowired
    private ClassesMapper classesMapper;

//    1.查询学生信息
    @Override
    public List<Student> findAll() {
        System.out.println("开始查询……");
        //1.1查询所有学生列表
        List<Student> students = studentMapper.selectByExample(null);
        //2.遍历所有的学生列表
        for (Student stu : students){
        //3.根据学生的班级编号cid查询所在班级cname
//            System.out.println("输出学号"+stu.getCid());
            Classes classes = classesMapper.selectByPrimaryKey(stu.getCid());
//            System.out.println("输出班级"+classes.getCname());
        //4.将班级传入student类中的cname
            stu.setCname(classes.getCname());
        }
        System.out.println("查询结束!!!!!");
        for (Student student1:students){
            System.out.println(student1.toString());
        }
        return students;
    }

    public List<Student> searchStudent(){
        StudentExample example = new StudentExample();
        System.out.println("创建完毕！");
        List<Student> students = studentMapper.selectByExample(example);
        for (Student stu : students){
            System.out.println("开始输出模糊查询的对象");
            //3.根据学生的班级编号cid查询所在班级cname
//            System.out.println("输出学号"+stu.getCid());
            Classes classes = classesMapper.selectByPrimaryKey(stu.getCid());
            stu.setCname(classes.getCname());
        }
        System.out.println("结束模糊查询");
        return students;
    }
}
